<template>
    <ContenFiled>   
        <div class="row justify-content-md-center">
            <div class="col-3">
                <form @submit.prevent="register">
                    <div class="mb-3">
                        <label for="username">用户名</label>
                        <input v-model="username" type="text" class="form-control" id="username" placeholder="请输入用户名">
                    </div>
                    <div class="mb-3">
                        <label for="Password">密码</label>
                        <input v-model="password" type="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>
                    <div class="mb-3">
                        <label for="confirmedPassword">确认密码</label>
                        <input v-model="confirmedpassword" type="password" class="form-control" id="confimedpassword" placeholder="请输入密码">
                    </div>
                    <small class="error_message">{{error_message}}</small>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
            </div>
        </div>
    </ContenFiled>
</template>

<script>
import ContenFiled from "@/components/ContentFiled.vue"
import $ from "jquery"
import {ref} from "vue"
import router from "@/router/index"

export default {
    components:{
        ContenFiled,
    },
    setup(){
        let username = ref("");
        let password = ref("");
        let confirmedpassword = ref("");
        let error_message = ref("");

        const register = () =>{
            $.ajax({
                url:"http://127.0.0.1:3000/user/account/register/",
                type:"post",
                data:{
                    username:username.value,
                    password:password.value,
                    confirmedPassword:confirmedpassword.value,
                },
                success(resp){
                    if(resp.error_message === "success"){
                        router.push({name:"user_account_login"})
                    }else{
                        error_message.value = resp.error_message;
                    }
                },
                error(resp){
                    console.log(resp);
                }
            })
        }

        return {
            username,
            password,
            confirmedpassword,
            error_message,
            register
        }
    }
    
}
</script>

<style scoped>
button{
    width: 100%;
}
.error_message{
    color: red;
}
</style>